home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / libkb100.zip / LIBKB-1.00 / SRC / KBTABLES.C < prev    next >
C/C++ Source or Header  |  1996-07-23  |  6KB  |  277 lines

  1. /* kbtables.c -- keyboard tables and high level access functions
  2.  * Copyright (C) 1995, 1996 Markus F.X.J. Oberhumer
  3.  * For conditions of distribution and use, see copyright notice in kb.h 
  4.  */
  5.  
  6.  
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <kb.h>
  10. #include "_kb.h"
  11. #include "_kbtable.hh"
  12. #include "_kbname.hh"
  13.  
  14.  
  15. #if defined(KB_LOCK_ALL_START)
  16. KB_LOCK_ALL_START(_libkb_kbtables)
  17. #endif
  18.  
  19.  
  20. const unsigned kb_version_id = KB_VERSION_ID;
  21. const char kb_version_string[] = KB_VERSION_STRING;
  22. const char kb_version_date[] = KB_VERSION_DATE;
  23. const char kb_copyright[] =
  24.     "libkb -- Copyright (C) 1995, 1996 Markus F.X.J. Oberhumer";
  25.  
  26.  
  27. /* Once again (17 Jan 1996) the Linux kernel sources have stopped
  28.  * me from making things too complicate. As this is a keyboard 
  29.  * library and not a re-write of an operating system, I have 
  30.  * simplified the tables to suit the basic needs.
  31.  * There are 4 tables that are set up for American keyboards.
  32.  * If you really want to do use local key mappings while the
  33.  * keyboard handler is active, use kb_keypress() and
  34.  * convert the code yourself.
  35.  */
  36.  
  37. #if !defined(_kb_plain_table)
  38. unsigned short _kb_plain_table[128];
  39. #endif
  40. #if !defined(_kb_shift_table)
  41. unsigned short _kb_shift_table[128];
  42. #endif
  43. #if !defined(_kb_control_table)
  44. unsigned short _kb_control_table[128];
  45. #endif
  46. #if !defined(_kb_alt_table)
  47. unsigned short _kb_alt_table[128];
  48. #endif
  49.  
  50.  
  51. /***********************************************************************
  52. // convert kb_keypress() to a keycode
  53. //
  54. // see comments for keycode() in mktables.c
  55. ************************************************************************/
  56.  
  57. unsigned kb_keycode(unsigned k)
  58. {
  59.     unsigned scan = k & 0x7f;
  60.  
  61.     if (KB_ANY_MASK(k, KB_SHIFT_ANY_ALT << 8))
  62.         return _kb_alt_table[scan];
  63.     else if (KB_ANY_MASK(k, KB_SHIFT_ANY_CONTROL << 8))
  64.         return _kb_control_table[scan];
  65.     else if (KB_ANY_MASK(k, KB_SHIFT_ANY_SHIFT << 8))
  66.         return _kb_shift_table[scan];
  67.     else
  68.         return _kb_plain_table[scan];
  69. }
  70.  
  71.  
  72. /***********************************************************************
  73. //
  74. ************************************************************************/
  75.  
  76. static unsigned long kb_inkey_keycode(unsigned key)
  77. {
  78.     if (key)
  79.         return kb_keycode(key) | ((unsigned long) key << 16);
  80.     else
  81.         return 0;
  82. }
  83.  
  84.  
  85. static unsigned kb_getkey_keycode(unsigned key)
  86. {
  87.     if (key)
  88.         return kb_keycode(key);
  89.     else
  90.         return 0;
  91. }
  92.  
  93.  
  94. /***********************************************************************
  95. //
  96. ************************************************************************/
  97.  
  98. unsigned long kb_inkey(void)
  99. {
  100.     unsigned key;
  101.     unsigned long code;
  102.  
  103.     if (!_kb_mode)
  104.         return kb_os_getkey();
  105.     while ((key = kb_keypress()) != 0)
  106.     {
  107.         code = kb_inkey_keycode(key);
  108.         if (code != 0)
  109.             return code;
  110.     }
  111.     return 0;
  112. }
  113.  
  114. unsigned long kb_inkey_i(void)
  115. {
  116.     unsigned key;
  117.     unsigned long code;
  118.  
  119.     if (!_kb_mode)
  120.         return kb_os_getkey();
  121.     while ((key = kb_keypress_i()) != 0)
  122.     {
  123.         code = kb_inkey_keycode(key);
  124.         if (code != 0)
  125.             return code;
  126.     }
  127.     return 0;
  128. }
  129.  
  130.  
  131. unsigned kb_getkey(void)
  132. {
  133.     unsigned key, code;
  134.  
  135.     if (!_kb_mode)
  136.         return kb_os_getkey();
  137.     while ((key = kb_keypress()) != 0)
  138.     {
  139.         code = kb_getkey_keycode(key);
  140.         if (code != 0)
  141.             return code;
  142.     }
  143.     return 0;
  144. }
  145.  
  146. unsigned kb_getkey_i(void)
  147. {
  148.     unsigned key, code;
  149.  
  150.     if (!_kb_mode)
  151.         return kb_os_getkey();
  152.     while ((key = kb_keypress_i()) != 0)
  153.     {
  154.         code = kb_getkey_keycode(key);
  155.         if (code != 0)
  156.             return code;
  157.     }
  158.     return 0;
  159. }
  160.  
  161.  
  162. unsigned kb_waitkey(void)
  163. {
  164.     if (!_kb_mode)
  165.         return kb_os_waitkey();
  166.  
  167.     while (!kb_kbhit())
  168.         _kb_usleep(1024);
  169.     return kb_getkey();
  170. }
  171.  
  172. unsigned kb_waitkey_i(void)
  173. {
  174.     if (!_kb_mode)
  175.         return kb_os_waitkey();
  176.  
  177.     while (!kb_kbhit_i())
  178.         _kb_usleep(1024);
  179.     return kb_getkey_i();
  180. }
  181.  
  182.  
  183. /***********************************************************************
  184. // kb_keycode, kb_bios_keycode and kb_os_keycode should return
  185. // the same values - and this on all platforms
  186. ************************************************************************/
  187.  
  188. /* convert a BIOS keycode */
  189. /* see: djgpp1/libsrc/c/dos/getkey.s */
  190. /* see: djgpp2/src/libc/pc_hw/kb */
  191.  
  192. unsigned kb_bios_keycode(unsigned k)
  193. {
  194.     unsigned key = k & 0xff;
  195.  
  196.     if (key == 0xe0)
  197.         key = (k >> 8) | 0x200;
  198.     else if (key == 0)
  199.         key = (k >> 8) | 0x100;
  200.  
  201.     return key;
  202. }
  203.  
  204.  
  205. /* convert a getch() keycode */
  206. unsigned kb_os_keycode(unsigned k1, unsigned k2)
  207. {
  208.     if (k2 == 0)
  209.         return k1;
  210.     
  211.     /* we cannot distinguish between keypad and cursor keys,
  212.      * so let's assume cursor keys */
  213.     if (k2 >= KB_SCAN_7_PAD && k2 <= KB_SCAN_PERIOD_PAD &&
  214.         _kb_prefix_scancode[k2])
  215.         k2 |= 0x200;
  216.     else
  217.         k2 |= 0x100;
  218.  
  219.     return k2;
  220. }
  221.  
  222.  
  223. /***********************************************************************
  224. //
  225. ************************************************************************/
  226.  
  227. void _kb_init_tables(void)
  228. {
  229. #if !defined(_kb_plain_table)
  230.     memcpy(_kb_plain_table,default_plain_table,sizeof(_kb_plain_table));
  231. #endif
  232. #if !defined(_kb_shift_table)
  233.     memcpy(_kb_shift_table,default_shift_table,sizeof(_kb_shift_table));
  234. #endif
  235. #if !defined(_kb_control_table)
  236.     memcpy(_kb_control_table,default_control_table,sizeof(_kb_control_table));
  237. #endif
  238. #if !defined(_kb_alt_table)
  239.     memcpy(_kb_alt_table,default_alt_table,sizeof(_kb_alt_table));
  240. #endif
  241. }
  242.  
  243.  
  244. /***********************************************************************
  245. //
  246. ************************************************************************/
  247.  
  248. const char *kb_keyname(int scan)
  249. {
  250.     return _kb_key_name[scan & 0x7f];
  251. }
  252.  
  253.  
  254. /***********************************************************************
  255. // locking
  256. ************************************************************************/
  257.  
  258. #if defined(KB_LOCK_ALL_END)
  259. KB_LOCK_ALL_END(_libkb_kbtables)
  260. #endif
  261.  
  262. int _libkb_kbtables_lock(void)
  263. {
  264.     int x = 0;
  265.  
  266. #if defined(KB_LOCK_ALL)
  267.     KB_LOCK_ALL(_libkb_kbtables,x);
  268. #endif
  269.  
  270.     return x;
  271. }
  272.  
  273.  
  274. /*
  275. vi:ts=4
  276. */
  277.